package cn.gotom.pinggu.action;

import java.util.List;

import org.apache.log4j.Logger;

import cn.gotom.coding.EncodingUtils;
import cn.gotom.core.pojos.User;
import cn.gotom.core.service.UniversalService;
import cn.gotom.core.web.Constants;
import cn.gotom.core.web.action.MenuInf;
import cn.gotom.core.web.action.SupportAction;
import cn.gotom.pinggu.pojos.Cppw;
import cn.gotom.pinggu.pojos.Cpzl;
import cn.gotom.pinggu.pojos.Trcc;
import cn.gotom.pinggu.pojos.Ylpw;
import cn.gotom.pinggu.service.DataService;
import cn.gotom.pinggu.service.TrccService;
import cn.gotom.pinggu.vo.Nsrxx;

import com.google.inject.Inject;

/**
 * 投入产出
 * 
 * @author <a href="mailto:pqixere@qq.com">裴绍国</a>
 * 
 */
public class TrccAction extends SupportAction implements MenuInf
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public String list()
	{
		if (trcc == null)
		{
			dataList = trccService.findAll(Trcc.class);
		}
		else
		{
			dataList = trccService.find(trcc.getNsrdm(), trcc.getNd(), trcc.getYf());
		}
		for (Trcc o : dataList)
		{
			EncodingUtils.encoding(o, "cp850", "GBK");
		}
		return Constants.SUCCESS;
	}

	public String input()
	{
		if (trcc == null)
		{
			trcc = new Trcc();
		}
		else
		{
			Trcc temp = trccService.get(trcc.getId());
			if (temp != null)
			{
				trcc = temp;
			}
		}
		findOptionsList();
		return Constants.INPUT;
	}

	public void findOptionsList()
	{
		cpzlList = universalService.findAll(Cpzl.class);
		cppwList = universalService.findAll(Cppw.class);
		ylpwList = universalService.findAll(Ylpw.class);
		for (Cpzl o : cpzlList)
		{
			EncodingUtils.encoding(o, "cp850", "GBK");
		}
		for (Cppw o : cppwList)
		{
			EncodingUtils.encoding(o, "cp850", "GBK");
		}
		for (Ylpw o : ylpwList)
		{
			EncodingUtils.encoding(o, "cp850", "GBK");
		}
	}

	public String remove()
	{
		try
		{
			log.debug("destroy  " + this);
			if (ids != null)
			{
				log.debug("remove id=" + ids.length + ids);
				for (String id : ids)
				{
					log.debug("remove id=" + id);
					if (trccService.exists(Trcc.class, id))
					{
						trccService.remove(Trcc.class, id);
					}
				}
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			log.error(ex.getMessage(), ex);
		}
		return list();
	}

	public String save()
	{
		Nsrxx nsrxx = dataService.getNsrxxByNsrdm(trcc.getNsrdm());
		if (nsrxx == null)
		{
			this.setActionmessage("请输入正确的纳税人代码！");
			findOptionsList();
			return Constants.INPUT;
		}
		User user = (User) this.getSession().get(Constants.USER_SESSION_KEY);
		trcc.setUser(user);
		trcc.setNsrdm(nsrxx.getNsrdm());
		trcc.setNsrmc(nsrxx.getNsrmc());
		EncodingUtils.encoding(trcc, "GBK", "cp850");
		trcc = (Trcc) trccService.persist(trcc);
		return Constants.LIST;
	}

	protected final Logger log = Logger.getLogger(getClass());

	@Inject
	private TrccService trccService;

	@Inject
	private DataService dataService;

	@Inject
	private UniversalService universalService;

	private String[] ids;

	private Trcc trcc;

	private List<Trcc> dataList;

	private List<Cpzl> cpzlList;

	private List<Cppw> cppwList;

	private List<Ylpw> ylpwList;

	@Override
	public String getName()
	{
		return "数据采集";
	}

	@Override
	public String getGroupName()
	{
		return "数据采集";
	}

	@Override
	public int getMenuSort()
	{
		return 200;
	}

	public String[] getIds()
	{
		return ids;
	}

	public void setIds(String[] ids)
	{
		this.ids = ids;
	}

	public Trcc getTrcc()
	{
		return trcc;
	}

	public void setTrcc(Trcc trcc)
	{
		this.trcc = trcc;
	}

	public List<Trcc> getDataList()
	{
		return dataList;
	}

	public void setDataList(List<Trcc> dataList)
	{
		this.dataList = dataList;
	}

	public List<Cpzl> getCpzlList()
	{
		return cpzlList;
	}

	public void setCpzlList(List<Cpzl> cpzlList)
	{
		this.cpzlList = cpzlList;
	}

	public List<Cppw> getCppwList()
	{
		return cppwList;
	}

	public void setCppwList(List<Cppw> cppwList)
	{
		this.cppwList = cppwList;
	}

	public List<Ylpw> getYlpwList()
	{
		return ylpwList;
	}

	public void setYlpwList(List<Ylpw> ylpwList)
	{
		this.ylpwList = ylpwList;
	}

}
